package com.example.adminserivce.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.adminserivce.Mapper.ResourceMapper;
import com.example.adminserivce.Service.ResourceService;
import com.example.adminserivce.domain.dto.ResourcePage;
import com.example.common.constants.RdsConstants;
import com.example.common.domain.pojo.Resources;
import com.example.common.utils.CacheClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resources> implements ResourceService {
    @Autowired
    private CacheClient cacheClient;
    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public List<Resources> getResource() {
        // 使用缓存穿透策略：查询存在性校验 + 空值缓存
        return cacheClient.queryWithPassThroughList(
                RdsConstants.RESOURCE_INFO,  // Redis 键前缀
                RdsConstants.ID_ALL_DATE,                          // 主键ID
                Resources.class,                 // 实体类类型
                this::getAllResources,                 // MyBatis-Plus 查询方法
                RdsConstants.CACHE_TTL,        // 正常数据缓存时间（分钟）
                TimeUnit.MINUTES,
                RdsConstants.CACHE_NULL_TTL,   // 空值缓存时间（分钟）
                TimeUnit.MINUTES
        );
    }

    @Override
    public List<Resources> getResourceByCourseId(String id) {
        return cacheClient.queryWithPassThroughList(
                RdsConstants.RESOURCE_INFO,  // Redis 键前缀
                id,                          // 主键ID
                Resources.class,                 // 实体类类型
                this::getAllResourcesById,                 // MyBatis-Plus 查询方法
                RdsConstants.CACHE_TTL,        // 正常数据缓存时间（分钟）
                TimeUnit.MINUTES,
                RdsConstants.CACHE_NULL_TTL,   // 空值缓存时间（分钟）
                TimeUnit.MINUTES
        );
    }

    @Override
    public Boolean DeleteResourceById(String id) {
        try {
            boolean isDeleted = super.removeById(id);
            if (isDeleted) {
                // 2. 删除 Redis 缓存
                String cacheKey = RdsConstants.RESOURCE_INFO + id; // "resource:Info" + id
                cacheClient.delete(cacheKey);
            }
            return isDeleted;
        } catch (Exception e) {
            log.error("删除资源失败, id={}", id, e);
            return false;
        }
    }

    @Override
    public Page<Resources> getResourcePage(ResourcePage queryWrapper) {
        // 获取分页对象
        Page<Resources> page = queryWrapper.toPage();
        // 构建查询条件
        QueryWrapper<Resources> wrapper = queryWrapper.buildQueryWrapper();
        // 执行查询
        return baseMapper.selectPage(page, wrapper);
    }


    public List<Resources> getAllResources(String id) {
        QueryWrapper<Resources> queryWrapper = new QueryWrapper<>();
        return list(queryWrapper);
    }

    public List<Resources> getAllResourcesById(String courseId) {
        QueryWrapper<Resources> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        return list(queryWrapper);
    }

}
